package com.hqyj.smvc.handler;

import com.github.pagehelper.PageInfo;
import com.hqyj.smvc.pojo.Employee;
import com.hqyj.smvc.pojo.Idcard;
import com.hqyj.smvc.service.EmployeeService;
import com.hqyj.smvc.service.IdcardService;
import com.hqyj.smvc.service.impl.EmployeeServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

@Controller
@Slf4j
public class IdcardHandler {
    @Autowired
    @Qualifier("employeeService")
    private EmployeeService employeeService;
    @Autowired
    @Qualifier("idcardService")
    private IdcardService idcardService;

    //登录到主页面
   @RequestMapping(value = "handleRequest.action", method = RequestMethod.POST)
    public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       log.info("Handler>>>handleRequest,start……");
        //获取前端数据
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        //将获取到的数据封装进Employee对象
        Employee employee = new Employee();
        employee.setEmployee_username(username);
        employee.setEmployee_password(password);
        //调用service层方法
        Employee login = employeeService.login(employee);
        request.setAttribute("employee", login);
        //判断返回值是否为空
        if(login != null){
            //不为空跳转展示页面
            request.getRequestDispatcher("/WEB-INF/employee/index.jsp").forward(request, response);
        } else {
            //为空，用户不存在，跳转登录页面重新登录，并提示用户名或密码错误，请重新输入
            request.setAttribute("error", "用户名或密码错误，请重新输入");
            request.getRequestDispatcher("/login.jsp").forward(request, response);
        }
       log.info("Handler>>>handleRequest,end……");
    }



    //查看所有身份证
    @RequestMapping(value = "findAllIdcard.action", method = RequestMethod.GET)
    public void findAllIdcard(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        log.info("Handler>>>findAllIdcard,start……");
        //第一次访问第一页
        int page = 1;
        String pageNum = request.getParameter("pageNum");
        //获取访问页码
        if(pageNum != null) {
            page = Integer.parseInt(pageNum);
        }
        //调用service层方法
        List<Idcard> idcards = idcardService.showTheIdcard(page);
        //分页工具
        PageInfo<Idcard> idcardPageInfo = new PageInfo<>(idcards);
        if(idcards != null){
            //传入域对象
            request.setAttribute("idcards", idcards);
            request.setAttribute("idcardPageInfo", idcardPageInfo);
            request.getRequestDispatcher("/WEB-INF/employee/findAllIdcard.jsp").forward(request, response);
        }
        log.info("Handler>>>findAllIdcard,end……");
    }

    //查询没有身份证的雇员
    @RequestMapping(value = "appendIdcard.action", method = RequestMethod.GET)
    public void appendIdcard(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        log.info("Handler>>>appendIdcard,start……");
       //查询没有身份证的雇员
        List<Employee> employees = employeeService.noIdcardEmployee();
        request.setAttribute("noIdcard", employees);
        request.getRequestDispatcher("/WEB-INF/employee/appendIdcard.jsp").forward(request, response);
        log.info("Handler>>>appendIdcard,end……");
    }

    //雇员添加身份证跳转
    @RequestMapping(value = "jumpInsertCard.action", method = RequestMethod.GET)
    public String jumpInsertCard(){
       return "/WEB-INF/employee/insertEmployeeIdcard.jsp";
    }

    //添加身份证
    @RequestMapping(value = "addIdcard.action", method = RequestMethod.POST)
    public void addIdcard(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        log.info("Handler>>>addIdcard,start……");
        String idcard_num = request.getParameter("idcard_num");
        String idcard_starttime = request.getParameter("idcard_starttime");
        String idcard_endtime = request.getParameter("idcard_endtime");
        String id = request.getParameter("factor");
        System.out.println(id);
        if("请选择".equals(id)){
            //没有选择雇员不允许添加
            request.setAttribute("warning", "请选择一个雇员");
            appendIdcard(request, response);
        } else {
            int i = Integer.parseInt(id);
            Idcard idcard = new Idcard();
            Employee employee = new Employee();
            employee.setEmployee_id(i);
            idcard.setIdcard_num(idcard_num);
            idcard.setIdcard_starttime(idcard_starttime);
            idcard.setIdcard_endtime(idcard_endtime);
            idcard.setEmployee(employee);
            System.out.println(idcard);
            idcardService.appendIdcard(idcard);
            findAllIdcard(request, response);
        }
        log.info("Handler>>>addIdcard,end……");


    }

    //删除身份证
    @RequestMapping(value = "removeIdcard.action", method = RequestMethod.GET)
    public void removeIdcard(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        log.info("Handler>>>removeIdcard,start……");
        String idcard_id = request.getParameter("idcard_id");
        int i = Integer.parseInt(idcard_id);
        System.out.println(idcard_id);
        int i1 = idcardService.removeEmployee(i);
        if (i1 > 0) {
            findAllIdcard(request, response);
        }
        log.info("Handler>>>removeIdcard,end……");
    }

    //跳转编辑页面
    @RequestMapping(value = "jumpPage.action", method = RequestMethod.GET)
    public void jumpPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        log.info("Handler>>>jumpPage,start……");
       String userid = request.getParameter("userid");
        int i = Integer.parseInt(userid);
        System.out.println(i);
        Idcard idcard = idcardService.demandIdcard(i);
        request.setAttribute("demand", idcard);
        request.getRequestDispatcher("/WEB-INF/employee/amendIdcard.jsp").forward(request, response);
        log.info("Handler>>>jumpPage,end……");
    }

    //修改身份信息
    @RequestMapping(value = "amendIdcard.action", method = RequestMethod.POST)
    public void amendIdcard(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        log.info("Handler>>>amendIdcard,start……");
       String idcard_id = request.getParameter("idcard_id");
        int i = Integer.parseInt(idcard_id);
        System.out.println(i);
        String idcard_starttime = request.getParameter("idcard_starttime");
        String idcard_endtime = request.getParameter("idcard_endtime");
        Idcard idcard = new Idcard();
        idcard.setIdcard_id(i);
        idcard.setIdcard_starttime(idcard_starttime);
        idcard.setIdcard_endtime(idcard_endtime);
        idcardService.compileIdcard(idcard);
        findAllIdcard(request, response);
        log.info("Handler>>>amendIdcard,end……");
    }

}
